package com.xq.utils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;

/**
 * 数据库代码生成器
 * <p>输入需要生成的表格即可生成结构</p>
 * @version 0.1.0
 * @author wuven
 * @since 0.1.0
 * @date 2025/6/27 15:00
 **/

public class MpGeneratorUtil {

    public static void main(String[] args) {

        String[] tables={"users"};
        String URL="jdbc:mysql://localhost/myblog";
        generate(URL,"root","root","wuven","myblog-service/src/main/java","com.xq","null","myblog-service/src/main/resources/mapper",tables);

    }

    private static void generate(String url,String username,String password,String author,String outFile,String parent,String module,String mapperFile,String... name) {
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author(author)  //指定作者
                            .outputDir(outFile)  //指定输出目录
                            .disableOpenDir()   //禁止打开输出目录
                            .fileOverride()     //覆盖已有文件
                            .dateType(DateType.TIME_PACK);  //使用java.time包下的日期类型
                })
                .packageConfig(builder -> {
                    builder.parent(parent) //设置父包名
                            //.moduleName(module)  //设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, mapperFile))   //mapper.xml生成路径
                    ;
                })
                .strategyConfig(builder -> {
                    builder.addInclude(name) // 设置需要生成的表名,支持多个
                    //        .addTablePrefix("t_", "sys_"); // 设置过滤表前缀
                            .entityBuilder()
                            .enableLombok() //启动lombook
                            .enableTableFieldAnnotation();   //生成tableField注解
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎
                .execute();

    }

}
